/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admintool.imp03_ui_components;

import admintool.imp03_data.IMP03_Candidate;
import admintool.imp03_data.IMP03_Company;
import admintool.imp03_data.IMP03_Message;
import admintool.imp03_data.IMP03_Platform;
import admintool.imp03_data.IMP03_Skill;
import admintool.imp03_data.IMP03_Tip;
import admintool.imp03_data.IMP03_Vacancy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Administrators
 */
public class MainSelectionListener implements ListSelectionListener{

    private javax.swing.JList list_subSelection;
    private javax.swing.JList list_mainSelection;
    private JPanel panel_parameters;
    private Connection dbConnection = null;

    /**
     * 
     */
    public MainSelectionListener() {
    }
    
    /**
     * 
     * @param list_mainSelection
     * @param list_subSelection
     * @param panel_parameters
     * @param dbConnection
     */
    public MainSelectionListener(JList list_mainSelection, JList list_subSelection, JPanel panel_parameters, Connection dbConnection) {
        this.list_mainSelection = list_mainSelection;
        this.list_subSelection = list_subSelection;
        this.panel_parameters = panel_parameters;
        this.dbConnection = dbConnection;
    }

    public void valueChanged(ListSelectionEvent e) {
        if(!e.getValueIsAdjusting() && list_mainSelection.getSelectedValue() != null){
            Statement statement = null;
            ResultSet resultset = null;
            try {
                String mainSelection = (String)list_mainSelection.getSelectedValue();
                statement = dbConnection.createStatement();

                //empty the sub-list
                list_subSelection.setModel(new MyListModel(new MyCollection()));
                
                //empty the paramter-window
                panel_parameters.removeAll();
                panel_parameters.repaint();

                if (mainSelection.equalsIgnoreCase("PLATFORM")){
                    //// PLATFORM ////
                    ArrayList<IMP03_Platform> platforms = new ArrayList<IMP03_Platform>();
                    platforms.add(IMP03_Platform.getEmptyPlatformObject());
                    platforms.addAll(IMP03_Platform.getAllAvailablePlatformsFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(platforms.toArray())));
                } else if(mainSelection.equalsIgnoreCase("VACANCY")){
                    //// VACANCY ////
                    ArrayList<IMP03_Vacancy> vacancies = new ArrayList<IMP03_Vacancy>();
                    vacancies.add(IMP03_Vacancy.getEmptyVacancyObject());
                    vacancies.addAll(IMP03_Vacancy.getAllAvailableVacanciesFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(vacancies.toArray())));
                } else if(mainSelection.equalsIgnoreCase("COMPANY")){
                    //// COMPANY ////
                    ArrayList<IMP03_Company> companies = new ArrayList<IMP03_Company>();
                    companies.add(IMP03_Company.getEmptyCompanyObject());
                    companies.addAll(IMP03_Company.getAllAvailableCompaniesFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(companies.toArray())));
                } else if(mainSelection.equalsIgnoreCase("SKILL")){
                    //// SKILL ////
                    ArrayList<IMP03_Skill> skills = new ArrayList<IMP03_Skill>();
                    skills.add(IMP03_Skill.getEmptySkillObject());
                    skills.addAll(IMP03_Skill.getAllAvailableSkillsFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(skills.toArray())));
                } else if(mainSelection.equalsIgnoreCase("CANDIDATE")){
                    //// CANDIDATE ////
                    ArrayList<IMP03_Candidate> candidates = new ArrayList<IMP03_Candidate>();
                    candidates.add(IMP03_Candidate.getEmptyCandidateObject());
                    candidates.addAll(IMP03_Candidate.getAllAvailableCandidatesFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(candidates.toArray())));
                } else if(mainSelection.equalsIgnoreCase("MESSAGE")){
                    //// MESSAGE ////
                    ArrayList<IMP03_Message> messages = new ArrayList<IMP03_Message>();
                    messages.add(IMP03_Message.getEmptyMessageObject());
                    messages.addAll(IMP03_Message.getAllAvailableMessagesFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(messages.toArray())));
                } else if(mainSelection.equalsIgnoreCase("TIP")){
                    //// TIP ////
                    ArrayList<IMP03_Tip> tip = new ArrayList<IMP03_Tip>();
                    tip.add(IMP03_Tip.getEmptyTipObject());
                    tip.addAll(IMP03_Tip.getAllAvailableTipsFromDB(true));
                    
                    //Update list of available platforms
                    list_subSelection.setModel(new MyListModel(new MyCollection(tip.toArray())));
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            } finally {
                try {
                    if(resultset != null){
                        resultset.close();
                    }
                    if(statement != null){
                        statement.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }

            }
        }
    }

}
